<template>
  <div class="main">
    <!-- tabbar导航 -->
    <van-tabbar route v-model="active">
      <van-tabbar-item icon="gem-o" to="/">首页</van-tabbar-item>
      <van-tabbar-item icon="search" to="/classify">分类</van-tabbar-item>
      <van-tabbar-item icon="cart-o" to="/shopp">购物车</van-tabbar-item>
      <van-tabbar-item icon="manager-o" to="/mine">我</van-tabbar-item>
    </van-tabbar>
    <!-- 头部区域 -->
    <header class="header">
      <!-- 头部搜索 -->
      <div class="header_sear" v-show="flag">
        <Search></Search>
        <div class="icon">
          <van-icon name="service-o" color="#fff" size=".6rem" />
        </div>
      </div>
      <!-- 轮播图 -->
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="item, index in newSrc" :key="index">
          <img :src="item" alt="" class="img">
        </van-swipe-item>
      </van-swipe>
    </header>
    <!-- 图片滚动区域 -->
    <div class="swiper_slide">
      <div class="slide-img">
        <img :src="item.imgUrl" alt="" class="img2" v-for="(item, index) in data.list" :key="item.categoryId">
      </div>
    </div>
    <!-- 精品列表 -->
    <Tite></Tite>
    <!-- 商品列表 -->
    <Etion :splke="sky"></Etion>
    <!-- 商品选项列表 -->
    <div class="tabList">
      <!-- <van-tabs v-model:active="active1">
        <van-tab v-for="(item, index) in data.tablist" :key="index" @click="tabtoggle(index)">
          <template #title> {{ item.title }}</template>
          <div class="tab_con">
            <ul class="con-cen">
              <li class="con-list" v-for="(item, index) in item.list" :key="index">
                <cpmmodity :item="item"></cpmmodity>
              </li>
            </ul>
          </div>
        </van-tab>
      </van-tabs> -->
      <!-- 切换标题 -->
      <ul class="oul">
        <li class="tab_li" v-for="(item, index) in data.tablist" :key="index" @click="tabtoggle(index)"
          :class="indexof == index ? 'active' : 'tab_li'">
          <div class="bigtit">{{ item.title }}</div>
          <div class="txt">{{ item.name }}</div>
          <div class="slide_box"></div>
        </li>
      </ul>
      <!-- /*渲染内容*/ -->
      <div class="tab_con">
        <ul class="con-cen">
          <li class="con-list" v-for="(item, index) in newArr" :key="index">
            <cpmmodity :sub="index" :item="item"></cpmmodity>
          </li>
        </ul>
      </div>
    </div>
  </div>
  <div class="redRoue" v-show="shopStore.isHidden">{{ shopStore.shopList.length }}</div>
</template>

<script setup>
//引入组件
import Cpmmodity from '../components/Commodity.vue'
import Search from '../components/header_search.vue'
import Tite from '../components/titleicon.vue'
import Etion from '../components/spike-area.vue'

//引入方法
import { computed, ref, onMounted, reactive, getCurrentInstance, nextTick } from 'vue'
import { swiper } from '@/api/home.js'
import { useRouter } from 'vue-router'
import { useShopStore } from '../store/shop'
const shopStore = useShopStore()

//定义响应式数据
let flag = ref(true); //头部显示隐藏
let active = ref(0);
let indexof = ref(0);
let sky = ref([])
let newArr = ref([])
let data = reactive({
  list: [],
  tablist: [],
  tabitem: [{ list: [] }]
})
const scrollRef = ref()

let path = './image/banner'
let swiperSrc = ['5.fb21a19b.jpg', '6.3b281aa3.jpg', '7.f187995a.jpg', '8.2b15b757.jpg']
let swiper2 = ['./image/11.jpg', './image/22.png', './image/33.png']
//轮播图图片操作
let newSrc = swiperSrc.map(item => item = path + item)

const { proxy } = getCurrentInstance();
const router = useRouter()
onMounted(() => {
  //获取接口数据
  swiper().then((res) => {
    data.list = res.data.banners2;
    data.list = data.list.map((item, index) => {
      item.imgUrl = swiper2[index]
      return item
    })
    data.tablist = res.data.tabList;
    data.tabitem = res.data.tabList
    newArr.value = data.tabitem[0].list

  })

  document.addEventListener('scroll', () => {
    let scrollT = document.documentElement.scrollTop
    if (scrollT > 300) {
      flag.value = false
    }
    else {
      flag.value = true
    }
  })

})


//选项卡切换
function tabtoggle(i) {
  indexof.value = i
  newArr.value = data.tabitem[i].list
}
// //商品详情传递数据
// function toggSwitch(item) {
//   sessionStorage.setItem('deilDate', JSON.stringify(item))
//   router.push({ path: '/detail' })
// }



</script>

<style lang="scss" scoped>
.redRoue {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #d8182d;
  position: fixed;
  bottom: 67px;
  right: 32%;
  z-index: 100;
  color: #fff;
  font-size: 24px;
  text-align: center;
}

.main {
  width: 750px;
  overflow-x: hidden;
  height: auto;
  background: #EFEFF4;
}

/*显示 隐藏*/


/*tab选项卡*/
.tabList {
  width: 100%;
  height: 90px;
  margin-bottom: 200px;

  .oul {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: space-between;
    flex-wrap: nowrap;

    .tab_li {
      width: 20%;
      height: 100%;
      text-align: center;
      color: #3a3a3a;

      .bigtit {
        font-size: 34px;
        font-weight: bold
      }

      .txt {
        font-size: 24px;
      }

      .slide_box {
        width: 80px;
        height: 5px;
        border-radius: 5px;
        margin: 0 auto;
        background: transparent
      }
    }

    .active {
      color: #d8182d;

      .slide_box {
        background: red
      }
    }
  }

  /*内容*/
  .tab_con {
    width: 100%;
    margin: 0 auto;
    box-sizing: border-box;
    height: auto;
    background: #EFEFF4;
    padding: 20px 20px;

    .con-cen {
      width: 100%;
      height: 100%;
      // background: #fff;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;

      .con-list {
        width: 46%;
        height: 510px;
        background: #fff;
        border-radius: 10px;
        border: 1px solid #ddd;
        box-sizing: border-box;
        margin-bottom: 16px;
      }
    }
  }
}


van-swipe {
  width: 750px;
  height: 500px !important;
}

.header {
  width: 750px;
  height: 500px;

  .header_sear {
    width: 750px;
    height: 40px;
    position: fixed;
    top: 20px;
    z-index: 100;
    display: flex;
    flex-wrap: nowrap;
    justify-content: space-around;

  }

  .img {
    width: 100%;
    height: 100%
  }

}

.swiper_slide {
  width: 750px;
  height: 180px;
  overflow-y: hidden;
  margin: 60px 0 10px;

  .slide-img {
    width: 1400px;
    height: 100%;
    display: flex;
    flex-wrap: nowrap;

    .img2 {
      width: 440px;
      height: 100%;
      float: left;
      margin: 0 16px;
      border-radius: 10px;
    }
  }

}
</style>
